<template>
  <el-dialog
    :title="!dataForm.teacherId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="教师名称"></el-input>
    </el-form-item>
    <el-form-item label="自我介绍" prop="selfIntroduction">
      <el-input v-model="dataForm.selfIntroduction" placeholder="自我介绍"></el-input>
    </el-form-item>
    <el-form-item label="咨询人数" prop="consultingNum">
      <el-input v-model="dataForm.consultingNum" placeholder="咨询人数"></el-input>
    </el-form-item>
    <el-form-item label="工作年限" prop="workingYears">
      <el-input v-model="dataForm.workingYears" placeholder="工作年限"></el-input>
    </el-form-item>
    <el-form-item label="工作介绍" prop="workIntroduction">
      <el-input v-model="dataForm.workIntroduction" placeholder="工作介绍"></el-input>
    </el-form-item>
    <el-form-item label="老师图片" prop="photo">
         <single-upload ref="singleUpload" v-model="dataForm.photo"></single-upload>
    </el-form-item>
    <el-form-item label="描述" prop="descs">
      <el-input v-model="dataForm.descs" placeholder="描述"></el-input>
    </el-form-item>
     <el-form-item label="性别" prop="sex">
        <el-select v-model="dataForm.sex" placeholder="请选择">
            <el-option  v-for="(item) in sexs" :key="item.value" :label="item.label" :value="item.value"></el-option>
         </el-select>
       </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>
  
<script>
import singleUpload from "@/components/upload/singleUpload.vue";
  export default {
    components : {singleUpload},
    data () {
      return {
        visible: false,
        dataForm: {
          teacherId: 0,
          name: '',
          selfIntroduction: '',
          photo: '',
          createDate: '',
          createBy: '',
          lastUpdateDate: '',
          lastUpdateBy: '',
          descs: '',
          consultingNum: '',
          workingYears: '',
          workIntroduction: '',
          sex: '',
          userId: this.$cookie.get("userId")
        },
        sexs: [{
          value: '0',
          label: '男'
        }, {
          value: '1',
          label: '女'
        }],
        dataRule: {
          name: [
            { required: true, message: '教师名称不能为空', trigger: 'blur' }
          ],
          selfIntroduction: [
            { required: true, message: '自我介绍不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          descs: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          consultingNum: [
            { required: true, message: '咨询人数不能为空', trigger: 'blur' }
          ],
          workingYears: [
            { required: true, message: '工作年限不能为空', trigger: 'blur' }
          ],
          workIntroduction: [
            { required: true, message: '工作介绍不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.teacherId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.teacherId) {
            this.$http({
              url: this.$http.adornUrl(`/heartSelf/shTeacher/teacherInfo/${this.dataForm.teacherId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.shTeacher.name
                this.dataForm.selfIntroduction = data.shTeacher.selfIntroduction
                this.dataForm.photo = data.shTeacher.photo
                this.dataForm.descs = data.shTeacher.descs
                this.dataForm.consultingNum = data.shTeacher.consultingNum
                this.dataForm.workingYears = data.shTeacher.workingYears
                this.dataForm.workIntroduction = data.shTeacher.workIntroduction
                this.dataForm.sex = data.shTeacher.sex
                this.dataForm.userId=data.shTeacher.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/heartSelf/shTeacher/${!this.dataForm.teacherId ? 'saveTeacher' : 'updateTeacher'}`),
              method: 'post',
              data: this.$http.adornData({
                'teacherId': this.dataForm.teacherId || undefined,
                'name': this.dataForm.name,
                'selfIntroduction': this.dataForm.selfIntroduction,
                'photo': this.dataForm.photo,
                'createDate': this.dataForm.createDate,
                'createBy': this.dataForm.createBy,
                'descs': this.dataForm.descs,
                'consultingNum': this.dataForm.consultingNum,
                'workingYears': this.dataForm.workingYears,
                'workIntroduction': this.dataForm.workIntroduction,
                'sex': this.dataForm.sex,
                'userId':this.dataForm.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
